﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       ReadImage
- 机器名称：       YTE
- 命名空间：       LD.Tool.Vision
- 文 件 名：       ReadImage
- 创建时间：       2022/7/14 15:11:16
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using HalconDotNet;
using LD.Host;
using LD.Vision;
using System;
using System.IO;

namespace LD.Tool
{
    [Tool(FrmParam = "ToolCreateShapeFrm", Text = "创建模板", ToolType = ToolEnum.Vision)]

    class CreateShape : BaseTool
    {
        public override OpResult Execute(TB_Tool model)
        {
            stopwatch.Start();
            try
            {
                var param = Newtonsoft.Json.JsonConvert.DeserializeObject<CreateShapeParam>(model.Param);
                if (param != null)
                {
                    if (CameraFactory.Instance.CameraList.ContainsKey(param.Camera))
                    {
                        var camera = CameraFactory.Instance.CameraList[param.Camera];
                        var image = FrmVisionMain.Instance.HWindow[camera.Name].Image;// .HWindow.Image;
                        if (image != null)
                        {
                            var roiRegion = camera.Roi[param.Roi].getRegion();
                            var regionImage = image.ReduceDomain(roiRegion);
                            var shapeModel = regionImage.CreateShapeModel(new HTuple("auto"),
                                                                    -180d,
                                                                    180d,
                                                                    new HTuple("auto"),
                                                                    new HTuple("auto"),
                                                                    "use_polarity",
                                                                    new HTuple("auto"),
                                                                    new HTuple("auto"));
                            var xld = shapeModel.GetShapeModelContours(1);

                            roiRegion.AreaCenter(out HTuple row, out HTuple column);
                            //创建转换矩阵
                            HHomMat2D hommat2D = new HHomMat2D();
                            hommat2D.VectorAngleToRigid(0, 0, 0, row, column, 0);
                            var xldCont = hommat2D.AffineTransContourXld(xld);

                            FrmVisionMain.Instance.HWindow[camera.Name].DispObj(xldCont);

                            //保存模板
                            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Vision\{camera.Name}");
                            string pathModel = Path.Combine(path, $@"Model");
                            shapeModel.WriteShapeModel(Path.Combine(pathModel, $"{param.Name}.ldm"));
                            camera.SaveShape($"{param.Name}.ldm", shapeModel);

                            xldCont.Dispose();
                            hommat2D = null;
                            xld.Dispose();
                            shapeModel.Dispose();

                            isSuccess = true;
                        }

                    }
                }

                Log(model, "运行结束", LogLevel.一般);
            }
            catch (Exception ex)
            {
                Log(model, ex.Message, LogLevel.严重);
            }
            finally
            {
                stopwatch.Stop();
            }

            return new OpResult() { IsSuccess = isSuccess, ExecuteTime = stopwatch.ElapsedMilliseconds };
        }
    }
}
